package com.qst.crop.controller;

import com.alipay.api.domain.UserDetails;
import com.qst.crop.common.Result;
import com.qst.crop.common.StatusCode;
import com.qst.crop.entity.Bank;
import com.qst.crop.entity.Intention;
import com.qst.crop.entity.Recommend;
import com.qst.crop.security.entity.JwtUser;
import com.qst.crop.service.BankService;
import com.qst.crop.service.FinanceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.util.List;
///融资
@Api(tags = "融资模块接口")
@RestController
@RequestMapping("/finance")
@CrossOrigin
public class FinanceController {

    @Autowired
    private FinanceService financeService;

    @Autowired
    private BankService bankService;

    // 查询融资意向
    @ApiOperation(value = "查询融资意向")
    @GetMapping("/selectIntentionByName")
    public Result selectIntentionByName() {
        JwtUser principal = (JwtUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        String name = principal.getUsername();
        List<Intention> intentions = financeService.selectIntentionByName(name);
        return new Result<>(true, StatusCode.OK, "查询成功", intentions);
    }

    // 查询银行情报
    @ApiOperation(value = "查询银行情报")
    @GetMapping("/selectbank")
    public Result selectBank() {
        List<Bank> banks = bankService.selectAllBank();
        return new Result(true, StatusCode.OK, "查询成功", banks);
    }

    // 添加融资意向
    @ApiOperation(value = "添加融资意向")
    @PostMapping("/insertIntentionByName")
    public Result insertIntentionByName(@RequestBody Intention intention) {
        JwtUser principal = (JwtUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        String name = principal.getUsername();
        intention.setUserName(name);
        financeService.insertIntentionByName(intention);
        return new Result<>(true, StatusCode.OK, "添加成功");
    }

    // 修改融资意向
    @ApiOperation(value = "修改融资意向")
    @PutMapping("/updateIntentionByName")
    public Result updateIntentionByName(@RequestBody Intention intention) {
        JwtUser principal = (JwtUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        String name = principal.getUsername();
        intention.setUserName(name);
        System.out.println("修改融资意向");
        System.out.println(intention);
        financeService.updateIntentionByName(intention);
        return new Result<>(true, StatusCode.OK, "修改成功");
    }

    // 删除融资意向
    @ApiOperation(value = "删除融资意向")
    @DeleteMapping("/deleteIntentionByName")
    public Result deleteIntentionByName() {
        JwtUser principal = (JwtUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        String name = principal.getUsername();
        financeService.deleteIntentionByName(name);
        return new Result<>(true, StatusCode.OK, "删除成功");
    }

    // 查询推荐融资人
    @ApiOperation(value = "查询推荐融资人")
    @GetMapping("/selectRecommendByName")
    public Result selectRecommendByName() {
        JwtUser principal = (JwtUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        String name = principal.getUsername();
        List<Recommend> list = financeService.selectRecommend(name);
        return new Result<>(true, StatusCode.OK, "查询成功", list);
    }
}
